package com.jd.controller;

import com.jd.bo.AddressBO;
import com.jd.bo.SubmitOrderBO;
import com.jd.enums.OrderStatusEnum;
import com.jd.enums.PayMethod;
import com.jd.pojo.OrderStatus;
import com.jd.pojo.UserAddress;
import com.jd.service.AddressService;
import com.jd.service.OrderService;
import com.jd.utils.CookieUtils;
import com.jd.utils.JDJSONResult;
import com.jd.utils.MobileEmailUtils;
import com.jd.vo.MerchantOrdersVO;
import com.jd.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@Api(value = "订单相关",tags = {"订单相关的api接口"})
@RestController
@RequestMapping("orders")
public class OrderController extends BaseController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private RestTemplate restTemplate;



    @ApiOperation(value = "用户下单",notes = "用户下单",httpMethod = "POST")
    @PostMapping("/create")
    public JDJSONResult create(@RequestBody SubmitOrderBO submitOrderBO,
                               HttpServletRequest request,
                               HttpServletResponse response) {

        if(submitOrderBO.getPayMethod()!= PayMethod.ALIPAY.type
                &&submitOrderBO.getPayMethod()!= PayMethod.WEIXIN.type){
            return JDJSONResult.errorMsg("支付方式不支持");
        }

        //1 创建订单
        OrderVO orderVO=orderService.createOrder(submitOrderBO);
        String orderId=orderVO.getOrderId();


        //2 创建订单以后，移除购物车中已结算（已提交）的商品
        /**
         * 1001
         * 2002 ->用户购买
         * 3003 ->用户购买
         * 4004
         *
         */
        //TODO 整合redis之后，完善购物车中的已结算的商品,并且同步前端的Cookie
        CookieUtils.setCookie(request,response,FOODIE_SHOP_CART,"",true);



        //3、向支付中心发送当前订单，用于保存支付中心的订单数据
        MerchantOrdersVO merchantOrdersVO=orderVO.getMerchantOrdersVO();
        merchantOrdersVO.setReturnUrl(payReturnUrl);

        //为了方便测试购买，所以所有的支付金额都统一改为1分钱


        //构建httpheader
        HttpHeaders headers=new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("imoocUserId","imooc");
        headers.add("password","imooc");

        HttpEntity<MerchantOrdersVO> entity=new HttpEntity<>(merchantOrdersVO,headers);
        ResponseEntity<JDJSONResult> responseEntity=restTemplate.postForEntity(paymentUrl,entity,JDJSONResult.class);
        JDJSONResult paymentResult=responseEntity.getBody();
        if(paymentResult.getStatus()!=200){
            return JDJSONResult.errorMsg("支付中心订单创建失败，请联系管理员");
        }
        return JDJSONResult.ok(orderId);
    }


    /**
     * 支付中心异步通知
     * @param merchOrderId
     * @return
     */
    @PostMapping("/notifyMerchantOrderPaid")
    public Integer notifyMerchantOrderPaid(String merchOrderId){
            orderService.updateOrderStatus(merchOrderId, OrderStatusEnum.WAIT_DELIVEER.type);
            return HttpStatus.OK.value();
    }

    @PostMapping("getPaidOrderInfo")
    public JDJSONResult getPaidOrderInfo(String orderId) {
        OrderStatus orderStatus = orderService.queryOrderStatusInfo(orderId);
        return JDJSONResult.ok(orderStatus);
    }



}
